package com.jerehedu.myapplication34.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jerehedu.myapplication34.R;
import com.jerehedu.myapplication34.entrty.Book;

import java.util.List;

/**
 * Created by lenovo on 2016/11/3.
 */
public class MyGridAdapter extends BaseAdapter {
    private List<Book> mData;
    private Context ctx;

    public MyGridAdapter(List<Book> mData, Context ctx) {
        this.mData = mData;
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Book book=mData.get(position);
        ViewHolder viewHolder=null;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView= LayoutInflater.from(ctx).inflate(R.layout.my_grid_item,null,false);
            viewHolder.title=(TextView)convertView.findViewById(R.id.textView);
            viewHolder.img=(ImageView)convertView.findViewById(R.id.imageView);
            viewHolder.checkBook=(ImageView)convertView.findViewById(R.id.checkBook);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        if(book.getImg()!=null&&!book.getImg().equals("")){
            Glide.with(ctx).load(book.getImg()).into(viewHolder.img);
            viewHolder.title.setVisibility(View.GONE);
        }else{
            viewHolder.title.setText(book.getName());
            viewHolder.img.setVisibility(View.GONE);
        }
        if(book.getIsShow()==0){
            viewHolder.checkBook.setVisibility(View.GONE);
        }else{
            viewHolder.checkBook.setVisibility(View.VISIBLE);
        }
        if(book.getIsCheck()==0){
            viewHolder.checkBook.setImageResource(R.mipmap.hl);
        }else{
            viewHolder.checkBook.setImageResource(R.mipmap.hm);
        }
        return convertView;
    }

    public class  ViewHolder{
        TextView title;
        ImageView img;
        ImageView checkBook;
    }
}
